<%@ page import="com.cdvtc.flower.dao.CategoryDao" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.cdvtc.flower.dao.impl.CategoryDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Category" %>
<%@ page import="java.util.List" %>
<%@ page import="com.cdvtc.flower.model.Products" %>
<%@ page import="com.cdvtc.flower.dao.ProductsDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.ProductsDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Merchants" %>
<%@ page import="com.cdvtc.flower.dao.MerchantsDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.MerchantsDaoImpl" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/5/29
  Time: 17:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品信息</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/main.css">

</head>
<body>
<!--<jsp:include page="commons/header.jsp"></jsp:include>-->
    <div class="container">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">
                    修改商品信息
                </h3>
            </div>
            <div class="panel-body" style="padding: 20px;">
                <form action="ProductsModifyServlet" class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="product_id" class="col-sm-2 control-label">商品编号：</label>
                        <%
                            // 这里是一个简化的例子，通常您会使用DAO或Service层来执行此操作
                            String product_id = request.getParameter("product_id"); // 获取product_id字符串
                                    ProductsDao productsDao = new ProductsDaoImpl();
                            Products product = productsDao.getProductById(Integer.parseInt(product_id)); // 获取单个商品
                            pageContext.setAttribute("product", product); // 而不是整个products列表
                            Integer originalCategoryId = null;
                            Integer originalMerchantId = null;
                            originalCategoryId = product.getCategory_id();
                            pageContext.setAttribute("originalCategoryId", originalCategoryId);
                            originalMerchantId = product.getMerchant_id();
                            pageContext.setAttribute("originalMerchantId", originalMerchantId);
                        %>
                        <div class="col-sm-10">
                            <input type="text" readonly class="form-control" name="product_id" id="product_id" placeholder="请输入商品编号" value="${product.product_id}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="product_name" class="col-sm-2 control-label">名称：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="product_name" id="product_name" placeholder="请输入商品名称" value="${product.product_name}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="category_id" class="col-sm-2 control-label">种类：</label>
                        <div class="col-sm-10">
<%--                            <input type="text" class="form-control" name="category_name" id="category_name" placeholder="请输入商品名称" value="${product.category_id}">--%>
                            <%
                            CategoryDao categoryDao = new CategoryDaoImpl();
                            List<Category> categoryList; // 更改变量名以避免混淆
                            categoryList = categoryDao.getAllCategories();
                            pageContext.setAttribute("categoryList", categoryList); // 使用新的变量名
                        %>
                            <select class="form-control" name="category_id" id="category_id">
                                <option value="">请选择花卉颜色</option>
                                <c:forEach var="category" items="${categoryList}" varStatus="status">
                                    <c:choose>
                                        <c:when test="${not empty originalCategoryId and originalCategoryId == category.category_id}">
                                            <!-- 如果当前category的ID等于originalCategoryId，则设置为selected -->
                                            <option value="${category.category_id}" selected="selected">${category.category_name}</option>
                                        </c:when>
                                        <c:otherwise>
                                            <!-- 否则，只是普通的option -->
                                            <option value="${category.category_id}">${category.category_name}</option>
                                        </c:otherwise>
                                    </c:choose>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="image" class="col-sm-2 control-label">图片：</label>
                        <div class="col-sm-10">
                            <input type="file" class="form-control" name="image" id="image" placeholder="请选择图片" accept="image/jpg,image/jpeg,image/png" value="${product.image}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="original_price" class="col-sm-2 control-label">原价：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="original_price" id="original_price" placeholder="请输入原价" value="${product.original_price}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sale_price" class="col-sm-2 control-label">售价：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="sale_price" id="sale_price" placeholder="请输入售价" value="${product.sale_price}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="description" class="col-sm-2 control-label">简述：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="description" id="description" placeholder="请输入商品简述" value="${product.description}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="merchant_id" class="col-sm-2 control-label">商家：</label>
                        <div class="col-sm-10">
                            <%--                            <input type="text" class="form-control" name="category_name" id="category_name" placeholder="请输入商品名称" value="${product.category_id}">--%>
                            <%
                                MerchantsDao merchantsDao = new MerchantsDaoImpl();
                                List<Merchants> merchantsList; // 更改变量名以避免混淆
                                merchantsList = merchantsDao.getALLMerchants();
                                pageContext.setAttribute("merchantsList", merchantsList); // 使用新的变量名
                            %>
                            <select class="form-control" name="merchant_id" id="merchant_id">
                                <option value="">请选择商家</option>
                                <c:forEach var="merchant" items="${merchantsList}" varStatus="status">
                                    <c:choose>
                                        <c:when test="${not empty originalMerchantId and originalMerchantId == merchant.merchant_id}">
                                            <!-- 如果当前category的ID等于originalCategoryId，则设置为selected -->
                                            <option value="${merchant.merchant_id}" selected="selected">${merchant.store_name}</option>
                                        </c:when>
                                        <c:otherwise>
                                            <!-- 否则，只是普通的option -->
                                            <option value="${merchant.merchant_id}">${merchant.store_name}</option>
                                        </c:otherwise>
                                    </c:choose>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sold" class="col-sm-2 control-label">是否上架：</label>
                        <div class="col-sm-10">
                            <select class="form-control" name="sold" id="sold">
                                <c:choose>
                                    <c:when test="${product.sold == true}">
                                        <option value="true" selected="selected">true</option>
                                        <option value="false">false</option>
                                    </c:when>
                                    <c:otherwise>
                                        <option value="true">true</option>
                                        <option value="false" selected="selected">false</option>
                                    </c:otherwise>
                                </c:choose>
                            </select> <!-- 这里添加了闭合的 select 标签 -->
                        </div> <!-- 这里是 col-sm-10 的闭合标签 -->
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">确定</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>


